$(document).ready(function() {
    $('#summernote').summernote({
        height: 300,
        tabsize: 2,
        lang: 'zh-CN',
        placeholder: '请输入问题的详细描述...',
        callbacks:{
            //summernote内部定义了一个特殊方法，会自动在用户选中图片后运行
            onImageUpload:function(files){
                let file = files[0]; //参数是个数组，实际上数组的第一个元素就是我们选中的图片

                let form = new FormData();
                form.append("imageFile",file);
                $.ajax({
                    url: "/upload/image",
                    method: 'post',
                    data: form,
                    cache:false,//本次提交内容不缓存
                    //使用ajax执行文件上传必须设置contentType processData为false，否则会按照字符串数据处理上传；
                    contentType: false,
                    processData: false,
                    success:function(r){
                        console.log(r);
                        if(r.code == OK){
                            let img = new Image();
                            img.src = r.message;
                            //将设置好路径的img添加到富文本编辑器中
                            $('#summernote').summernote('insertNode',img)
                        }else {
                            alert(r.message)
                        }
                    }
                })
            }
        }
    });
    $('select').select2({placeholder:'请选择...'});
});

let answersApp = new Vue({
    el: '#answersApp',
    data: {
        answers: []
    },
    methods: {
        postComment:function(answerId){
            //console.log(answerId);
            let textarea=$("#addComment"+answerId+" textarea");
            let content=textarea.val();
            //console.log(content);
            let data={
                answerId:answerId,
                content:content
            }
            $.ajax({
                url:"/v1/comment",
                method:'post',
                data:data,
                success:function (r){
                    console.log(r)
                    if (r.code == CREATED) {
                        alert(r.message)
                    } else {
                        alert(r.message)
                    }
                }
            })
        },
        loadAnswers: function () {
            let questionId = location.search;
            if (!questionId) {
                alert('必须获得问题ID')
                return;
            }
            questionId = questionId.substring(1);
            $.ajax({
                url: '/v1/answers/question/' + questionId,
                method: 'get',
                success: function (r) {
                    console.log(r)
                    if (r.code == OK) {
                        answersApp.answers = r.data
                        answersApp.updateDuration();
                    } else {
                        alert(r.message)
                    }
                }
            })
        },
        updateDuration: function () {
            for (let i = 0; i < this.answers.length; i++) {
                addDuration(this.answers[i]);
            }
        }
    },
    created: function () {
        this.loadAnswers();
    }
})
let questionApp=new Vue({
    el:'#questionApp',
    data:{
        detailquestion:{}
    },
    methods:{
        detailQuestion:function () {
            let search = location.search;
            let s = search.substring(1);
            console.log(s)
            $.ajax({
                url:'/v1/questions/detail/'+s,
                method:'get',
                success:function (r){
                    console.log(r)
                    if(r.code=200){
                        questionApp.detailquestion=r.data
                        //console.log(detailquestion)
                    }

                }
            })
        }, updateDuration:function(){
            //创建问题的时间 ms
            let createtime = new Date(this.question.createtime).getTime(); // 创建时间 -- ms
            //当前时间
            let now = new Date().getTime();
            let duration = now - createtime;
            if(duration < 60*1000){
                this.question.duration = '刚刚';
            }else if(duration < 60*60*1000){
                this.question.duration = (duration/1000/60).toFixed(0) + '分钟以前';
            }else if(duration < 24*60*60*1000){
                this.question.duration = (duration/1000/60/60).toFixed(0) + '小时以前';
            }else {
                this.question.duration = (duration/1000/60/60/24).toFixed(0) + '天以前';
            }
        }

    },
    created:function (){
        this.detailQuestion();
    }
})

let postAnswerApp=new Vue({
    el:'#postAnswerApp',
    data: {
        message:'',
        hasError:false
    },
    methods: {
        postAnswer: function () {
            this.hasError = false;
            let content = $('#summernote').val();
            let search = location.search;
            let s = search.substring(1);

            if (!s) {
                this.message = '没有问题ID'
                this.hasError = true;
                return;
            }
            //教师回答内容
            if (!content) {
                this.message = '必须填写回答内容'
                this.hasError = true;
                return;
            }
            let data = {
                questionId: s,
                content: content
            }
            $.ajax({
                url: '/v1/answers',
                method: 'post',
                data: data,
                success: function (r) {
                    if (r.code == CREATED) {
                        //获取数据
                        let answer=r.data
                        //保存到数组
                        answersApp.answers.push(answer)

                        $('#summernote').summernote('reset')
                        postAnswerApp.message = r.message
                        postAnswerApp.hasError = true;
                    } else {
                        postAnswerApp.message = r.message
                        postAnswerApp.hasError = true;
                    }
                }
            })
        }
    }
})